package com.syyo.gulimall.order.listener;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.rabbitmq.client.Channel;
import com.syyo.gulimall.order.config.pay.AlipayTemplate;
import com.syyo.gulimall.order.domain.entity.OrderEntity;
import com.syyo.gulimall.order.domain.entity.OrderReturnReasonEntity;
import com.syyo.gulimall.order.domain.vo.PayAsyncVo;
import com.syyo.gulimall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: wangzhong
 * @Date: 2020/7/30 10:54
 * @Description:
 */
@RestController
public class OrderPayListener {

    @Autowired
    OrderService orderService;

    @Autowired
    AlipayTemplate alipayTemplate;

    /**
     *
     * 买家账号  hemqst7845@sandbox.com
     *
     * 支付宝支付的回调接口
     */
    @PostMapping("/payed/notify")
    public String handleAliPay(PayAsyncVo vo, HttpServletRequest request) throws AlipayApiException {
        Map<String, String[]> parameterMap = request.getParameterMap();
//        for (String key : parameterMap.keySet()) {
//            String parameter = request.getParameter(key);
//            System.out.println("参数名：" + key + "==>" + "参数值：" + parameter);
//        }

        // 验证签名
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (String name : requestParams.keySet()) {
            String[] values = requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayTemplate.getAlipay_public_key(),
                alipayTemplate.getCharset(), alipayTemplate.getSign_type()); //调用SDK验证签名

        if (signVerified) {
            System.out.println("签名验证成功...");
            //去修改订单状态
            //  处理支付宝的返回结果
            String result = orderService.handleAliPay(vo);
            return result;
        } else {
            System.out.println("签名验证失败...");
            return "error";
        }
    }

    @GetMapping("/payed/a")
    public String handleAliPay1(HttpServletRequest request) {

        return "a";
    }

    @GetMapping("/payed/b")
    public String handleAliPay2(HttpServletRequest request) {

        return "b";
    }
}
